package com.mdxx.xczd.ui.presenter;

import com.google.gson.Gson;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.bean.GetUserResult;
import com.mdxx.xczd.bean.UserBean;
import com.mdxx.xczd.bean.UserBeanResult;
import com.mdxx.xczd.ui.contract.SelectUserContract;
import com.mdxx.xczd.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import java.util.List;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MD-02 on 2017/2/6.
 */

public class SelectUserPresenter extends SelectUserContract.Presenter{

    @Override
    public void getUserRequest() {
        mModel.getUserData()
                .subscribe(new Observer<GetUserResult>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                /*Logger.d("e="+e.toString());*/
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(GetUserResult getUserResult) {
                        Logger.json("getUserResult=="+ new Gson().toJson(getUserResult));
                        if("0".equals(getUserResult.code)){
                            List<UserBean> userBeanList = getUserResult.data;
                            mView.returnUserData(userBeanList);
                        }else{
                            ToastUtil.showShort(getUserResult.errorMsg);
                        }
                    }
                });
    }

    @Override
    public void getLoginRequest(String username, String password) {
        mModel.getLoginData(username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(UserBeanResult userBeanResult) {
                        if("0".equals(userBeanResult.code)){
                            UserBean userBean = userBeanResult.data;
                            App.userBean = userBean;
                            App.USERID = userBean.empNum;
                            ToastUtil.showShort("登录成功");
                            mView.returnLoginData();
                        }else{
                            ToastUtil.showShort(userBeanResult.errorMsg);
                        }
                    }
                });
    }
}
